﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Application;
using Application.Dao;
using Ay.Framework.WPF.Controls;
using AYQQMGR2018.Views.Shared;
using Event;
using org.diordna.eventbus;
using 小工具.Views.Shared.creator;

namespace 小工具.Views
{
    /// <summary>
    /// _ViewStartView.xaml 
    /// 创建时间：2017/9/5 11:57:44
    /// </summary>
    public partial class MainView : AyWindow, IEventHandler<BaseEvent>
    {

        public MainView()
        {
            InitializeComponent();
            Init();
        }

        private void Init()
        {

            EventBus.Instance.Subscribe(this);

            InitSystemToolsGrid();
            InitApplicationGrid();
        }

        protected override void OnClosed(EventArgs e)
        {
            base.OnClosed(e);
            EventBus.Instance.Unsubscribe(this);
        }

        private void MoveWindow(object sender, MouseButtonEventArgs e)
        {
            DragMove();
        }

        private void InitSystemToolsGrid()
        {
            System.System[] tools = System.SystemTools.GetTools();
            for (int i = 0; i < tools.Length; i++)
            {
                SystemToolCreator creator = new SystemToolCreator();
                AyToolBoxExtraRightButton btn = creator.CreateBtn(tools[i]);
                systemTools.Children.Add(btn);
            }
        }

        private void InitApplicationGrid()
        {
            applicationTools.Children.Clear();
            List<CApplication> apps = new AppDao().Select();
            foreach (CApplication app in apps)
            {
                ApplicationToolCreator creator = new ApplicationToolCreator();
                applicationTools.Children.Add(creator.CreateBtn(app));
            }
        }

        private void App_Drop(object sender, System.Windows.DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                Array pathes = (Array)e.Data.GetData(DataFormats.FileDrop);
                for (int i = 0; i < pathes.Length; i++)
                {
                    ApplicationToolCreator creator = new ApplicationToolCreator();
                    AyToolBoxExtraRightButton btn = creator.CreateBtn((string) pathes.GetValue(i));
                    applicationTools.Children.Add(btn);
                }
            }
        }

        public void Handle(BaseEvent evt)
        {
            switch (evt.Message)
            {
                case MainViewEvent.NOTIFY_APP_INIT:
                    InitApplicationGrid();
                    break;
                default:
                    break;
            }
        }

        private void RadioButton_Checked(object sender, RoutedEventArgs e)
        {
            RadioButton cb = sender as RadioButton;

            if (cb != null && svToolBox != null)
            {
                string placeBorder = cb.Tag as string;
                Border bd = svToolBox.FindName(placeBorder) as Border;
                if (bd != null)
                {
                    System.Windows.Point relativePoint = ScrollViewerExpand.GetRelativePoint(bd, mainContent);
                    svToolBox.ScrollToPosition(relativePoint.X, relativePoint.Y);
                }
            }
        }
    }
}